package com.toommi.refuse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.common.enums.RefuseTypeEnum;
import com.toommi.refuse.entity.RefuseType;
import com.toommi.refuse.entity.RefuseTypeExample;
import com.toommi.refuse.enums.RefuseTypeExceptionEnum;
import com.toommi.refuse.exception.RefuseTypeException;
import com.toommi.refuse.mapper.RefuseTypeMapper;
import com.toommi.refuse.service.RefuseTypeService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangg
 * @since 2019-07-26 10:04
 */
@Service
@AllArgsConstructor
public class RefuseTypeServiceImpl implements RefuseTypeService {

    private RefuseTypeMapper typeMapper;

    @Override
    public void addType(RefuseType refuseType) throws RefuseTypeException {

        if (StringUtils.isEmpty(refuseType.getName())) {
            throw new RefuseTypeException(RefuseTypeExceptionEnum.INVALID_TYPE_NAME_NULL);
        }
        if (StringUtils.isEmpty(refuseType.getType()) || !RefuseTypeEnum.hasValue(refuseType.getType())) {
            throw new RefuseTypeException(RefuseTypeExceptionEnum.INVALID_UP_TYPE_NULL);
        }
        if (StringUtils.isEmpty(refuseType.getPrice() + "")) {
            throw new RefuseTypeException(RefuseTypeExceptionEnum.INVALID_PRICE_NULL);
        }
        if (StringUtils.isEmpty(refuseType.getUnit())) {
            throw new RefuseTypeException(RefuseTypeExceptionEnum.INVALID_UNIT_NULL);
        }

        refuseType.setCreateTime(System.currentTimeMillis());
        refuseType.setUpdateTime(System.currentTimeMillis());

        typeMapper.insert(refuseType);
    }

    @Override
    public void deleteType(long id) {
        typeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> findTypes(String code) {


        return typeMapper.getTypesInfo(code);


    }

    @Override
    public RefuseType findDetail(long id) {
        return typeMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取细分类信息
     *
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> getTypesInfo(String id) {
        return typeMapper.getTypesInfo(id);
    }

    /**
     * 修改回收价
     *
     * @param request
     */
    @Override
    public void updatejfgz(HttpServletRequest request) {
        String zb = request.getParameter("zb");
        String zbid = request.getParameter("zbid");
        String sl = request.getParameter("sl");
        String slid = request.getParameter("slid");
        String pm = request.getParameter("pm");
        String pmid = request.getParameter("pmid");
        typeMapper.updatejfgzzb(zb, zbid);
        typeMapper.updatejfgzzb(sl, slid);
        typeMapper.updatejfgzzb(pm, pmid);
    }

    @Override
    public List<Map<String, Object>> getTypeNameList() {
        List<Map<String, Object>> list = typeMapper.getTypeNameList();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            //格式化价格，保留两位小数
            for (Map map : list) {
                String price = map.get("price").toString();
                BigDecimal bg = new BigDecimal(price);
                String priceformat = bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                map.put("price",priceformat);
            }
        }
        return list;
    }

    @Override
    public Object getTypeList(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<RefuseType> typeList = typeMapper.getTypeList();
        PageInfo<RefuseType> pageInfo = new PageInfo<>(typeList);
        Map<String, Object> result = new HashMap<>();
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", typeList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    @Override
    public RefuseType findOneById(Integer id) {
        return typeMapper.findOneById(id);
    }

    @Override
    public void updateTypeById(RefuseType type) {
        typeMapper.updateTypeById(type);
    }

    @Override
    public void addRefuseType(RefuseType refuseType) throws RefuseTypeException {
        if (StringUtils.isEmpty(refuseType.getName())) {
            throw new RefuseTypeException(RefuseTypeExceptionEnum.INVALID_TYPE_NAME_NULL);
        }
        if (StringUtils.isEmpty(refuseType.getType()) || !RefuseTypeEnum.hasValue(refuseType.getType())) {
            throw new RefuseTypeException(RefuseTypeExceptionEnum.INVALID_UP_TYPE_NULL);
        }
        if (StringUtils.isEmpty(refuseType.getPrice() + "")) {
            throw new RefuseTypeException(RefuseTypeExceptionEnum.INVALID_PRICE_NULL);
        }
        if (StringUtils.isEmpty(refuseType.getUnit())) {
            throw new RefuseTypeException(RefuseTypeExceptionEnum.INVALID_UNIT_NULL);
        }
        typeMapper.addType(refuseType);
    }

    @Override
    public int deleteTypeById(Integer id) {
        return typeMapper.deleteTypeById(id);
    }

    @Override
    public Object findTypesByCondition(int page, int limit, String type) {
        PageHelper.startPage(page, limit);
        List<RefuseType> typeList = typeMapper.findTypesByCondition(type);
        PageInfo<RefuseType> pageInfo = new PageInfo<>(typeList);
        Map<String, Object> result = new HashMap<>();
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", typeList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }
}
